package com.qfedu.knowledge.config;

import com.qfedu.knowledge.filter.CaptchaFilter;
import com.qfedu.knowledge.handle.MyAuthenticationFailureHandle;
import com.qfedu.knowledge.handle.MyAuthenticationSuccessHandle;
import com.qfedu.knowledge.handle.MyLogoutSuccessHandler;
import com.qfedu.knowledge.service.impl.MyUserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @ClassName SecurityConfig
 * @Author ykx
 * @Date 2024/4/20 9:54
 * @Version 1.0
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserDetailServiceImpl myUserDetailService;

    @Autowired
    private MyAuthenticationSuccessHandle myAuthenticationSuccessHandle;

    @Autowired
    private MyAuthenticationFailureHandle myAuthenticationFailureHandle;

    @Autowired
    private MyLogoutSuccessHandler myLogoutSuccessHandler;

    @Autowired
    private CaptchaFilter captchaFilter;

    private static final String[] URL_WHITELIST = {
            "/login",
            "/logout",
            "/captcha",
            "/password",
            "/image/**"
    };

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 开启跨域以及csrf攻击 关闭
        http.cors().and().csrf().disable()
                .formLogin()
                .loginProcessingUrl("/knowledge/login")
                .successHandler(myAuthenticationSuccessHandle)
                .failureHandler(myAuthenticationFailureHandle)
//                // session禁用配置
//                .and()
//                .sessionManagement()
//                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                // 退出配置
                .and()
                .logout()
                .logoutUrl("/knowledge/logout")
                .logoutSuccessHandler(myLogoutSuccessHandler)
                .and()
                .addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class)

        ;
    }

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailService);
    }

}
